<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class GuangboController extends AdminbaseController {

   // 广播列表
    function guangbo_lst(){
        $biaoti= trim(I("biaoti"));
        $stime = I("kaishi");
        $etime = I("jieshu");

        $p = I("p");
        $where = " 1 ";
        if(!empty($biaoti)){
            $where .= " and biaoti like '%{$biaoti}%' ";
        }
        if (!empty($stime)) {
            $time1 = strtotime($stime." "."00:00:00");
            $where .= " and {$time1} <= ctime";
        }
        if (!empty($etime)) {
            $time2 = strtotime($etime." "."23:59:59");
            $where .= " and ctime <= {$time2}";
        }

        $count =  M("guangbo")->where($where)->count();
        if($count==0){
            $p = 0;
        }else{
            if(empty($p)){
                $p = 1;
            }
        }
        $totalpage = ceil($count/10);
        $page = $this->page($count,10);
        $sd = ($p-1)*10;
//               var_dump($stime);die;
        $data =  M("guangbo")
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order("id desc")
            ->select();
        $this->assign("data",$data);
        $this->assign("biaoti",$biaoti);
        $this->assign("stime_kaishi",$stime);
        $this->assign("etime_jieshu",$etime);
        $this->assign("page", $page->show('Admin'));
        $this->assign("p",$p);
        $this->assign("sd",$sd);
        $this->assign("totalpage",$totalpage);
        $this->display();
    }

    // 广播添加
    function guangbo_add(){
         if($_POST){
             $dat['biaoti'] = I("biaoti");
             $contents = I("content");
             $dat['content'] = $contents;
             $dat['ctime'] = time();
              $data = M("account")->where("id != 1")->field("id")->select();

             foreach($data as $v){
                 $uid[] = $v['id'];
             }
             //  开始
             include "./rongyun/master/API/rongcloud.php";
             $appKey = 'kj7swf8okivm2';
             $appSecret = 'TAEhDLABQsMjf';
             $RongCloud = new \RongCloud($appKey,$appSecret,$format = 'json');

             $userid = 1;

            $RongCloud->message()->publishPrivate($userid, $uid, 'RC:TxtMsg',"{\"content\":\"{$contents}\",\"extra\":\"helloExtra\",\"duration\":20}", 'thisisapush', '{\"pushData\":\"hello\"}', '4', '0', '0', '0', '0');

             M("guangbo")->add($dat);
             $this->success("添加成功",U("Guangbo/guangbo_lst"));
         }else{
             $this->display();
         }
    }
    // 广播添加
    function guangbo_del(){
          $id = I("id");
          $r = M("guangbo")->where("id = $id")->delete();
          if($r){
              $this->success("删除成功",U("Guangbo/guangbo_lst"));
          }else{
              $this->error("删除失败");
          }
    }


}